﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("ADM"))
            {
                if (udm.createConnection() != null)
                {
                    if (!IsPostBack)
                    {
                        ddlAccountType.Items.Add(new ListItem("Student Accounts", "1"));
                        ddlAccountType.Items.Add(new ListItem("Employee Accounts", "2"));

                        GridView1.DataSource = udm.showAccount(udm.createConnection(), 1);
                        GridView1.DataBind();
                    }
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        GenerateDates gd = new GenerateDates();

        if (e.CommandName == "reset")
        {
            if (ddlAccountType.SelectedValue.Equals("1"))
            {
                if (!udm.resetPasswordStudent(udm.createConnection(),e.CommandArgument.ToString(),gd.EncodePassword("123456")))
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else if (ddlAccountType.SelectedValue.Equals("2"))
            {
                if (!udm.resetPasswordEmployee(udm.createConnection(), Convert.ToInt32(e.CommandArgument.ToString()), gd.EncodePassword("123456")))
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
        }
    }
    protected void ddlAccountType_SelectedIndexChanged(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;

        if (ddlAccountType.SelectedValue.Equals("1"))
        {
            GridView1.DataSource = udm.showAccount(udm.createConnection(), 1);
            GridView1.DataBind();
        }
        else if (ddlAccountType.SelectedValue.Equals("2"))
        {
            GridView1.DataSource = udm.showAccount(udm.createConnection(), 2);
            GridView1.DataBind();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        string username = txtUsername.Text;
        string id = txtID.Text;

        if (ddlAccountType.SelectedValue.Equals("1"))
        {
            if (udm.searchAccount(udm.createConnection(), id, firstName, lastName, username) != null)
            {
                GridView1.DataSource = udm.searchAccount(udm.createConnection(), id, firstName, lastName, username);
                GridView1.DataBind();
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }
        }
        else if (ddlAccountType.SelectedValue.Equals("2"))
        {
            if (id.Length == 0)
            {
                if (udm.searchAccountEmployee(udm.createConnection(), -1, firstName, lastName, username) != null)
                {
                    GridView1.DataSource = udm.searchAccountEmployee(udm.createConnection(), -1, firstName, lastName, username);
                    GridView1.DataBind();
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else
            {
                if (udm.searchAccountEmployee(udm.createConnection(), Convert.ToInt32(id), firstName, lastName, username) != null)
                {
                    GridView1.DataSource = udm.searchAccountEmployee(udm.createConnection(), Convert.ToInt32(id), firstName, lastName, username);
                    GridView1.DataBind();
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                } 
            }
        }
    }
}